package binding

import (
	"context"
	"encoding/json"
	"group-card/common/response"
	"group-card/rpc/card/cardclient"

	"group-card/api/internal/svc"
	"group-card/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type BindingAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBindingAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindingAddLogic {
	return &BindingAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BindingAddLogic) BindingAdd(req *types.AddBindingRequest) (resp *types.BindingResponse, err error) {
	_, err = l.svcCtx.Card.BindingAdd(l.ctx, &cardclient.BindingAddRequest{
		CardNumber:    req.CardNumber,
		CustomerId:    req.CustomerId,
		UserId:        req.UserId,
		BindingTime:   req.BindingTime,
		FaceValue:     req.FaceValue,
		Balance:       req.Balance,
		Status:        req.Status,
		ServiceNumber: req.ServiceNumber,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("添加卡片绑定失败,参数:%s,异常:%s", reqStr, err.Error())
		return &types.BindingResponse{Code: response.ErrCode, Message: "添加卡片绑定失败"}, nil
	}

	return &types.BindingResponse{
		Code:    response.SuccessCode,
		Message: "添加卡片绑定成功",
	}, nil

}
